//
//  WYHPrintForm.m
//  BluetoothPrint
//
//  Created by wang on 2018/1/24.
//  Copyright © 2018年 wang. All rights reserved.
//

#import "WYHPrintForm.h"
NSString *const Form_1 = @"┌";//@"┏";
NSString *const Form_2 = @"─";//@"━";
NSString *const Form_3 = @"┬";//@"┳";
NSString *const Form_4 = @"┐";//@"┓";
NSString *const Form_5 = @"├";//@"┣";
NSString *const Form_6 = @"┼";//@"╋";
NSString *const Form_7 = @"┤";//@"┫";
NSString *const Form_8 = @"└";//@"┗";
NSString *const Form_9 = @"┴";//@"┻";
NSString *const Form_10 = @"┘";//@"┛";
NSString *const Form_11 = @"│";//@"┃";
NSString *const Form_12 = @" ";

@interface WYHPrintForm ()
@property (nonatomic, assign) BOOL printRow;
@property (nonatomic, strong) NSArray *oldColsWidthArr;
@property (nonatomic, strong) NSArray *colsWidthArr;
@property (nonatomic, strong) NSMutableString *appendStr;
@property (nonatomic, assign) BOOL changeColsWidthArr;


/**
 //是否已经打印表格头
 */
@property (nonatomic, assign) BOOL isFormStart;

/**
 //是否已经打印表格尾
 */
@property (nonatomic, assign) BOOL isFormEnd;

/**
 //是否已经正确添加了行
 */
@property (nonatomic, assign) BOOL isFormRow;

@end


@implementation WYHPrintForm

- (instancetype)initFormWithchangeColsWidthArr:(NSArray *)colsWidthArr
{
    self = [super init];
    if (self) {
        _colsWidthArr = colsWidthArr;
    }
    return self;
}

//获取表格字符串
- (NSString *)getFormContent
{
    if (self.isFormStart) {
        //字符串已经开始才需要拼接
        if (self.isFormEnd == NO) {
            self.isFormEnd = YES;
            [self formEnd];
        }
    }
    if (self.isFormRow == NO) {
        //没有添加行 就返回nil
        return nil;
    }
    
    return [NSString stringWithFormat:@"%@",self.appendStr];
}

- (void)changeColsWidthArr:(NSArray *)colsWidthArr
{
//    LLog(@"%@",colsWidthArr);
    _changeColsWidthArr = YES;
    _oldColsWidthArr = _colsWidthArr;
    _colsWidthArr = colsWidthArr;
}
//\r\n 换行
//┏ ━ ┳ ━ ┳ ━┓
//表格开始
- (void)formStart
{
    self.isFormStart = YES;
    [self.appendStr appendString:Form_1];
    for (int list = 0; list < self.colsWidthArr.count; list++) {
        NSInteger count = [self.colsWidthArr[list] integerValue];
        for (int i = 0; i<count; i++) {
            [self.appendStr appendString:Form_2];
        }
        if (list == self.colsWidthArr.count -1) {
            [self.appendStr appendString:Form_4];
        }else{
            [self.appendStr appendString:Form_3];
        }
    }
        //换行
    [self.appendStr appendString:@"\r\n"];
}
//表格行结束
- (void)formRowEnd
{
    [self.appendStr appendString:Form_5];
    for (int list = 0; list < self.colsWidthArr.count; list++) {
        NSInteger count = [self.colsWidthArr[list] integerValue];
        for (int i = 0; i<count; i++) {
            [self.appendStr appendString:Form_2];
        }
        if (list == self.colsWidthArr.count -1) {
            [self.appendStr appendString:Form_7];
        }else{
            [self.appendStr appendString:Form_6];
        }
    }
        //换行
    [self.appendStr appendString:@"\r\n"];
}
//表格结束
- (void)formEnd
{
    self.isFormEnd = YES;
    [self.appendStr appendString:Form_8];
    for (int list = 0; list < self.colsWidthArr.count; list++) {
        NSInteger count = [self.colsWidthArr[list] integerValue];
        for (int i = 0; i<count; i++) {
            [self.appendStr appendString:Form_2];
        }
        if (list == self.colsWidthArr.count -1) {
            [self.appendStr appendString:Form_10];
        }else{
            [self.appendStr appendString:Form_9];
        }
    }
        //换行
    [self.appendStr appendString:@"\r\n"];
}
- (void)printRowLineColsWidthArr:(NSArray *)colsWidthArr oldColsWidthArr:(NSArray *)oldColsWidthArr
{
    if (oldColsWidthArr == nil||colsWidthArr == oldColsWidthArr) {
        [self formRowEnd];
        return;
    }
    
    
    //暂定未printArr oldPrintArr 一样大
    NSMutableArray *printArr = [WYHPrintForm printFormRowStr:colsWidthArr];
    NSMutableArray *oldPrintArr = [WYHPrintForm printFormRowStr:oldColsWidthArr];
    if(printArr.count != oldPrintArr.count){
        //有问题
        return;
    }
    for (int  i = 0; i<printArr.count; i++) {
        NSString *oldStr = oldPrintArr[i];
        NSString *str = printArr[i];
       if ([oldStr isEqualToString:Form_6]&&[str isEqualToString:Form_2]){
             [oldPrintArr replaceObjectAtIndex:i withObject:Form_9];
       }
       else if ([oldStr isEqualToString:Form_2]&&[str isEqualToString:Form_6]){
           [oldPrintArr replaceObjectAtIndex:i withObject:Form_3];
       }
       else{
          [oldPrintArr replaceObjectAtIndex:i withObject:oldStr];
        }
    }
    for (NSString *str in oldPrintArr) {
        [self.appendStr appendString:str];
    }
        [self.appendStr appendString:@"\r\n"];
    _oldColsWidthArr = _colsWidthArr;
}
//中间
+ (NSMutableArray *)printFormRowStr:(NSArray *)colsWidthArr
{
    //表格形式改变的打印
    NSMutableArray *printArr = [NSMutableArray array];
    [printArr addObject:Form_5];
     for (int list = 0; list < colsWidthArr.count; list++) {
         NSInteger count = [colsWidthArr[list] integerValue];
         for (int i = 0; i<count; i++) {
             [printArr addObject:Form_2];
         }
         if (list == colsWidthArr.count -1) {
             [printArr addObject:Form_7];
         }else{
             [printArr addObject:Form_6];
         }
     }
    return printArr;
}

/**
 添加行
 
 @param contentRow 填充行内容 超过列宽(按字符长度换行)
 @return 行高
 */
- (NSInteger)addRow:(NSArray *)contentRow
{
    if (self.isFormStart == NO) {
        self.isFormStart = YES;
        [self formStart];
    }
    
    
    if (contentRow == nil) {
        return 0;
    }
    if (contentRow.count != _colsWidthArr.count) {
        //错误，列和内容不匹配
        return 0;
    }
    self.isFormRow = YES;
    
    //复合的数组
    NSMutableArray *contentArrCompound = [NSMutableArray array];
    if (self.lineFeed) {
        //打印内容多的时候换行
        //检查需要多少行
        NSInteger line = 1;
        NSArray *contentArr = [NSMutableArray arrayWithArray:contentRow];
        NSMutableArray *tempArr = [NSMutableArray array];
        for (int i = 0; i < contentArr.count; i++) {
            NSString *content = contentArr[i];
            NSInteger count = [_colsWidthArr[i] integerValue];//汉字宽度
            NSMutableArray *rowArr = [WYHPrintForm returnLinSubStringByByteWithByteWidth:count*2 forStr:content];
            [tempArr addObject:rowArr];
            if (rowArr.count>line) {
                line = rowArr.count;
            }
        }
        for (int i = 0; i<line; i++) {
            NSMutableArray *tempContentArr = [NSMutableArray array];
            for (int j = 0; j<tempArr.count; j++) {
                NSMutableArray *rowArr = tempArr[j];
                if (rowArr.count>i) {
                    NSString *content = rowArr[i];
                    [tempContentArr addObject:content];
                }else{
                    [tempContentArr addObject:@""];
                }
            }
            [contentArrCompound addObject:tempContentArr];
        }
    }else{
        //不换行
        [contentArrCompound addObject:[NSArray arrayWithArray:contentRow]];
    }
    //打印底部线
    if (self.printRow == YES) {
        [self printRowLineColsWidthArr:self.colsWidthArr oldColsWidthArr:self.oldColsWidthArr];
    }
    for (NSArray *strArr in contentArrCompound) {
        [self.appendStr appendString:Form_11];
        for (int list = 0; list < self.colsWidthArr.count; list++) {
            NSInteger count = [self.colsWidthArr[list] integerValue];
            NSString *str = strArr[list];
            NSInteger byteCount = [WYHPrintForm getStrByte:str];
            if (byteCount<count*2) {
                for (int i = 0; i<count*2-byteCount; i++) {
                    str = [NSString stringWithFormat:@"%@%@",str,Form_12];
                }
            }else{
                //截取后的长度可能比实际小 因为最后一个是汉字的时候不一定被截取到
                NSString *subStr = [WYHPrintForm subStringByByteWithByteWidth:count*2 forStr:str];
                NSInteger subByteCount = [WYHPrintForm getStrByte:subStr];
                for (int i = 0; i<count*2-subByteCount; i++) {
                    str = [NSString stringWithFormat:@"%@%@",subStr,Form_12];
                }
            }
            [self.appendStr appendString:str];
            if (list == self.colsWidthArr.count -1) {
                [self.appendStr appendString:Form_11];
            }else{
                [self.appendStr appendString:Form_11];
            }
        }
        //换行
        [self.appendStr appendString:@"\r\n"];
    }
    self.printRow = YES;
    return contentArrCompound.count;
    
}
+ (NSMutableArray *)returnLinSubStringByByteWithByteWidth:(NSInteger)byteWidth forStr:(NSString *)str
{
    NSMutableArray *subStrArr = [NSMutableArray array];
    return [WYHPrintForm returnLinSubStringByByteWithByteWidth:byteWidth forStr:str subStrArr:subStrArr];
}

//按字符串截取后 可以截取多少次
+ (NSMutableArray *)returnLinSubStringByByteWithByteWidth:(NSInteger)byteWidth forStr:(NSString *)str subStrArr:(NSMutableArray *)subStrArr
{
    if (subStrArr == nil) {
        subStrArr = [NSMutableArray array];
    }
    if (str.length == 0) {
        [subStrArr addObject:@""];
        return subStrArr;
    }
    NSString *subStr = [WYHPrintForm subStringByByteWithByteWidth:byteWidth forStr:str];
     [subStrArr addObject:subStr];
    if (str.length>subStr.length) {
        NSString *residueStr = [str substringFromIndex:subStr.length];
        return [WYHPrintForm returnLinSubStringByByteWithByteWidth:byteWidth forStr:residueStr subStrArr:subStrArr];
    }else{
         return subStrArr;
    }
}

//按照字节截取字符串   //sum > index 最后一个是汉字 不截取这个汉字  使用的时候请拼接空格
+ (NSString *)subStringByByteWithByteWidth:(NSInteger)byteWidth forStr:(NSString *)str{
    if (str == nil) {
        str = @"";
    }
    
    NSInteger sum = 0;
    NSString *subStr = [[NSString alloc] init];
    for(int i = 0; i<[str length]; i++){
        unichar strChar = [str characterAtIndex:i];
        if(strChar < 256){
            sum += 1;
        }
        else {
            sum += 2;
        }
        if (sum >= byteWidth) {
            if (sum == byteWidth) {
                subStr = [str substringToIndex:i+1];
                return subStr;
            }else{
                //sum > index 最后一个是汉字 不截取这个汉字  使用的时候请拼接空格
                if (i == 0) {
                    i = 1;
                }
                subStr = [str substringToIndex:i];
                return subStr;
            }
        }
    }
    //最后字符串字节长度比byteWidth 小
    return [NSString stringWithFormat:@"%@",str];
}
//获取字符串的字节长度
+ (NSInteger)getStrByte:(NSString *)str
{
    if (str == nil) {
        return 0;
    }
    NSInteger length = [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    return (length-str.length)/2+str.length;
}

- (NSArray *)colsWidthArr
{
    if (_colsWidthArr == nil) {
        _colsWidthArr = [NSArray array];
    }
    return _colsWidthArr;
}
- (NSMutableString *)appendStr
{
    if (_appendStr == nil) {
        _appendStr = [NSMutableString string];
    }
    return _appendStr;
}

@end
